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.
Related
I have a very wonderful problem in my app. I have a text view in my activity which get a text from sqlite like this:
Hello
World!
But when I start my app for first time, My text view show the text like this:
Hello word!
When I go out from app and clear my app form RAM and then comeback to the app my text view show the text currectly like this:
Hello
World!
In fact, when onCreate method run for first time my text view text show in single line. I tried very ways like use recreate method:
public void recreate() {
this.recreate();
}
And in onCreate method:
recreate();
But my app crashed!
Is there a way to solve this problem? Thanks
My Ativity1:
String pattern = "'%" + inputSearch.getText().toString() + "%'";
final String sql = "SELECT * FROM " + TABLE + " WHERE " + MAANY + " LIKE " + pattern;
final SQLiteDatabase mydb = new MyDatabase2(EnglishActivity.this).getWritableDatabase();
final Cursor c = mydb.rawQuery(sql, null);
Cursor cT = mydb.rawQuery(" SELECT * FROM "+ TABLE + " WHERE "
+ TITLE + " LIKE ? " ,new String []{inputSearch.getText()+"%"});
Cursor c2=mydb.rawQuery(" SELECT * FROM "+ TABLE + " WHERE "
+ MAANY + " LIKE ? " ,new String []{inputSearch.getText()+"%"});
lv=(ListView)findViewById(R.id.enlist);
listItems=new ArrayList<String>();
lv2=(ListView)findViewById(R.id.enlist2);
listItems2=new ArrayList<String>();
array = new String[cT.getCount()];
array2 = new String[cT.getCount()];
array3 =new int[cT.getCount()];
int i = 0;
while(cT.moveToNext()){
String uname = cT.getString(cT.getColumnIndex(TITLE));
array[i] = uname;
String maany = cT.getString(cT.getColumnIndex(MAANY));
array2[i] = maany;
array3[i]=cT.getInt(cT.getColumnIndex(ID));
i++;
}
arrayMaany = new String[c.getCount()];
arrayMaany2 = new String[c.getCount()];
arrayMaany3 =new int[c.getCount()];
int ii = 0;
while(c.moveToNext()){
String uname = c.getString(c.getColumnIndex(TITLE));
arrayMaany[ii] = uname;
String maany = c.getString(c.getColumnIndex(MAANY));
arrayMaany2[ii] = maany;
arrayMaany3[ii]=c.getInt(c.getColumnIndex(ID));
ii++;
}
final ArrayList<String> forNum1 = new ArrayList<String>(Arrays.asList(array));
if(forNum1.size()==0){
lv2.setVisibility(View.VISIBLE);
} else if(forNum1.size()>0) {
lv2.setVisibility(View.GONE);
}
Typeface tf = Typeface.createFromAsset(getAssets(),"font/Harir Bold.otf");
lv.setAdapter(new EnCustomAdapter(EnglishActivity.this,array,array2,tf));
lv2.setAdapter(new
EnCustomAdapter(EnglishActivity.this,arrayMaany,arrayMaany2,tf));
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
String i=array[position];
String ii=array2[position];
int iii=array3[position];
SharedPreferences en = getApplicationContext().getSharedPreferences("enn1", 1);
final SharedPreferences.Editor editorEn = en.edit();
String enn = en.getString("enn2" , i);
enn=i;
editorEn.putString("enn2", i);
editorEn.commit();
SharedPreferences fa = getApplicationContext().getSharedPreferences("faa1", 1);
final SharedPreferences.Editor editorFa = fa.edit();
String faa = fa.getString("faa2" , ii);
faa=ii;
editorFa.putString("faa2", ii);
editorFa.commit();
SharedPreferences idd = getApplicationContext().getSharedPreferences("id1", 1);
final SharedPreferences.Editor editorId = idd.edit();
int iddd = idd.getInt("id2" , iii);
iddd=iii;
editorId.putInt("id2", iii);
editorId.commit();
startActivity(new Intent(EnglishActivity.this,ArMaany.class));
overridePendingTransition(R.anim.animation_zoom_in_speed, R.anim.animation_zoom_out_speed);
}
});
lv2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, final View view, int position, long id) {
String i=arrayMaany[position];
String ii=arrayMaany2[position];
int iii=arrayMaany3[position];
SharedPreferences en = getApplicationContext().getSharedPreferences("enn1", 1);
final SharedPreferences.Editor editorEn = en.edit();
String enn = en.getString("enn2" , i);
enn=i;
editorEn.putString("enn2", i);
editorEn.commit();
SharedPreferences fa = getApplicationContext().getSharedPreferences("faa1", 1);
final SharedPreferences.Editor editorFa = fa.edit();
String faa = fa.getString("faa2" , ii);
faa=ii;
editorFa.putString("faa2", ii);
editorFa.commit();
SharedPreferences idd = getApplicationContext().getSharedPreferences("id1", 1);
final SharedPreferences.Editor editorId = idd.edit();
int iddd = idd.getInt("id2" , iii);
iddd=iii;
editorId.putInt("id2", iii);
editorId.commit();
startActivity(new Intent(EnglishActivity.this,ArMaany.class));
overridePendingTransition(R.anim.animation_zoom_in_speed, R.anim.animation_zoom_out_speed);
}
});
c.close();
c2.close();
mydb.close();
}});
My Activity 2:
final String enmaany = getSharedPreferences("enn1", 1).getString("enn2", "");
final String famaany = getSharedPreferences("faa1", 1).getString("faa2", "");
final int id = getSharedPreferences("id1", 1).getInt("id2", 1);
tt.setText(enmaany);
ttt.setText(famaany);
My activity 1 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"
android:padding="5dp"
android:background="#drawable/background_word">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:id="#+id/arabic_et">
<ImageView
android:layout_height="40dp"
android:layout_width="40dp"
android:gravity="center"
android:id="#+id/bSearchEn"
android:src="#drawable/search_edit"
android:background="#drawable/search_selector"/>
<EditText
android:inputType="text|textNoSuggestions"
android:layout_height="match_parent"
android:ems="10"
android:layout_width="match_parent"
android:id="#+id/inputSearchEn"
android:background="#FFFFFF"
android:textSize="20dp"
android:gravity="center_vertical|start"
android:hint="#string/searchEn"
android:textStyle="bold"
android:layout_marginLeft="10dp"/>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/arabic_list"
android:layout_marginTop="5dp"
android:orientation="vertical">
<ListView
android:scrollbarSize="10dp"
android:scrollbarThumbVertical="#drawable/custom_scroll_style"
android:fastScrollEnabled="false"
android:divider="#88D4D4D4"
android:dividerHeight="1dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/enlist"/>
<ListView
android:scrollbarSize="10dp"
android:scrollbarThumbVertical="#drawable/custom_scroll_style"
android:fastScrollEnabled="false"
android:divider="#88D4D4D4"
android:dividerHeight="1dp"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/enlist2"/>
</LinearLayout>
</LinearLayout>
My activity 2 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"
android:orientation="vertical"
android:background="#FFFFFF"
android:id="#+id/lf"
android:gravity="right">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:weightSum="1.0">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight=".5"
android:gravity="left|center_vertical">
<ImageView
android:layout_width="40dp"
android:gravity="center"
android:id="#+id/fa_img"
android:layout_height="40dp"
android:padding="3dp"/>
<ImageView
android:layout_height="33dp"
android:layout_width="33dp"
android:background="#drawable/email_selector"
android:id="#+id/pronounce"/>
<ImageView
android:layout_height="40dp"
android:layout_width="40dp"
android:id="#+id/copy"
android:src="#drawable/copy_selector"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_weight=".5"
android:gravity="right">
<ImageView
android:layout_height="40dp"
android:layout_width="40dp"
android:src="#drawable/unlearn"
android:id="#+id/img_close"/>
</LinearLayout>
</LinearLayout>
<View
android:layout_height="1dp"
android:layout_width="match_parent"
android:background="#88D4D4D4"
android:layout_marginTop="5dp"/>
<ScrollView
android:scrollbars="none"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:id="#+id/tv_help"
android:gravity="center"
android:textColor="#color/c1"
android:textSize="25dp"
android:textStyle="bold"
android:typeface="monospace"/>
<com.mshrapps.bs.ArialMTBoldRegularTextView2
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:id="#+id/tv_fa"
android:gravity="right"
android:textColor="#color/c1"
android:textSize="20dp"
android:layout_marginTop="10dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Try add this to your TextView in the xml file
android:singleLine="false"
How to get value from dynamically created EditText in Android?
This is my dynamic view (XML file)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/background"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="#+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
And I am using LayoutInflater because I want perfect Designed layout which is not accomplish by me via setLayoutParams so this is the reason I am using LayoutInflater.
And here is my code:
LayoutInflater l = getLayoutInflater();
final LinearLayout mainActivity = (LinearLayout) findViewById(R.id.dynamic);
final View view = l.inflate(R.layout.dynamic_layout_waypoints, null);
buttonWayPoints = (Button) view.findViewById(R.id.btn_waypoint);
editTextWayPoints = (EditText) view.findViewById(R.id.et_waypoint);
mainActivity.addView(editTextWayPoints);
I am new to Android. Most of people suggest this solution but it's not worked for me, the issue is when I implement this code my ADD NEW EDIT TEXT BUTTON not respond me.
This is how I am trying to get inserted value but it returns value of only last created edit text:
public Cursor getPerson(String Query) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(Query, null);
return c;
}
sb is StringBuilder sb;, w is String w;
String queryWayPoints = "select * from route_table where trip_id = " + t;
Cursor s = myDb.getPerson(queryWayPoints);
int count3 = s.getCount();
for (int j = 0; j < count3; j++) {
s.moveToNext();
w = s.getString(s.getColumnIndex("waypoints"));
// se.append(w + "." + "00");
}
trip_name.setText(n);
trip_date.setText(d);
sb.append(w);
}
trip_route.setText(sb.toString());
Although, I didn't get what exactly your question is, still I'll try to help.
What exactly this code does is, on clicking the button on top an new layout(one that you wanted) will add to the activity screen at runtime, i.e an edittext with a button, and when you click on that button, A toast with the value of respective edittext will be shown. Hence solving the problem of getting the value of dynamically added edittext
MainActivity
public class MainActivity extends AppCompatActivity {
Button generateET;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout myLinearLay = (LinearLayout) findViewById(R.id.dynamic);
generateET = (Button) findViewById(R.id.generateBtn);
generateET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LayoutInflater l = getLayoutInflater();
final View viewToAdd = l.inflate(R.layout.to_add, null);
Button buttonWayPoints = (Button) viewToAdd.findViewById(R.id.btn_waypoint);
final EditText editTextWayPoints = (EditText) viewToAdd.findViewById(R.id.et_waypoint);
buttonWayPoints.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (editTextWayPoints.getText().toString() != null) {
Toast.makeText(MainActivity.this, editTextWayPoints.getText().toString(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "No text found", Toast.LENGTH_SHORT).show();
}
}
});
myLinearLay.addView(viewToAdd);
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.techmahindra.stackques.MainActivity">
<Button
android:id="#+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:text="add editText To layout" />
<ScrollView
android:layout_below="#+id/generateBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<LinearLayout
android:id="#+id/dynamic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
></LinearLayout>
</ScrollView>
</RelativeLayout>
to_add.xml(layout which will be inflated at runtime)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/et_waypoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Way Points"
android:padding="10dp"
android:textSize="16sp" />
<Button
android:id="#+id/btn_waypoint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Find" />
</LinearLayout>
There are some problems with your code. You're trying to add editTextWayPoints to mainActivity, but editTextWayPoints already has a parent, which is view. I think you should be adding view to mainActivity (mainActivity.addView(view)). Finally, to access the value of editTextWayPoints, you simply do editTextWayPoints.getText().toString();
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);
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
I just want to store a data in a listview then displaying it with a textview, i identified the problem, it's in fact this ligne:
data = date + " : " + y + "L/100KM\n"+ " " + value1 + "L "+ value2 + "KM\n";
Operations made:
1-if i delete the first or the second "\n" the listview indicates that there is not any datas to display.
2- if i let the two "\n" then the value1 and value2 are not displayed !
The oncreate function of the class that show the datas:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.histo);
context = getApplicationContext();
activity = this;
final SharedPreferences preferences = PreferenceManager
.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
fileDir = "" + preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
s = myIO.ReadFilePerLine(getApplicationContext(), fileDir + fileName);
updatelv(this);
ListView L = (ListView) findViewById(R.id.lv);
L.setTextFilterEnabled(true);
the code when tapping the calculate button in the main class:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
context = getApplicationContext();
Toast.makeText(carburant.this, "Bienvenue!", Toast.LENGTH_LONG).show();
data = "";
final EditText vol;
final EditText kil;
final EditText cons;
vol = (EditText) findViewById(R.id.volume2);
kil= (EditText) findViewById(R.id.kilometrage2);
cons= (EditText) findViewById(R.id.consom2);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value1 = (((EditText) findViewById(R.id.volume2))
.getText()).toString();
String value2 = (((EditText) findViewById(R.id.kilometrage2)).getText())
.toString();
if (value1 .equalsIgnoreCase("") && value2.equalsIgnoreCase(""))
{
Toast.makeText(carburant.this, "Veuillez vérifier les deux champs", Toast.LENGTH_SHORT).show();
}
else{
float q1=Float.parseFloat(vol.getText().toString());
float q2=Float.parseFloat(kil.getText().toString());
float x=((q1 / q2)* 100);
String y= Float.toString(x);
cons.setText(y);
SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
String date = format.format(new Date());
data = date + " : " + y + "L/100KM\n"+ " " + value1 + "L "+ value2 + "KM\n";
if (data != "" ) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
String fileName = getResources().getString(R.string.fileName);
String fileDir = ""+ preferences.getString("login", "") + "."+ preferences.getString("marque", "") + ".";
myIO.WriteSettings(context, fileDir + fileName, data);
data = "";
The list_item XML:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="16sp" >
Histo.XML:
<?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" android:layout_weight="1"
android:id="#+id/ViewHisto">
<ListView android:id="#+id/lv" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1" />
</LinearLayout>
Main.XML:
<LinearLayout android:id="#+id/layoutPrincipal"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:id="#+id/volume1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Volume(en Litres):">
</TextView>
<EditText android:id="#+id/volume2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric="integer|decimal"
android:hint="#string/vol"
>
</EditText>
<TextView android:id="#+id/kilometrage1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Kilometrage(en Kilomètres) :">
</TextView>
<EditText android:id="#+id/kilometrage2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric="integer|decimal"
android:hint="#string/kil">
</EditText>
<TextView android:id="#+id/consom1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Consommation moyenne(en Litres/100KM) :">
</TextView>
<EditText android:id="#+id/consom2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:textSize="18sp"
android:numeric="integer|decimal"
android:hint="#string/cons">
</EditText>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:text="Calculer"
android:id="#+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</Button>
<Button
android:text="Reset"
android:id="#+id/button2"
android:layout_marginLeft="280px"
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
</Button>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Did you consider creating a View to through an Adapter containing two TextViews to you display it correctly? It's the best approach.