I'm writing an Android lab and somehow my bottom button and text enter part are not showing. I'm using Linear Layout. it shows in the XML file design mode but doesn't show when running the program. Can someone help me find out what the problem is?
I tried to add a pic of what my design page and program run pic but it's not letting me, when I run the program I can see the two strings that I added to an array, but just not see the button and plain text at the bottom
here is my code, I have my java file code list below the xml code:
<ListView
android:id="@ id/listView"
android:layout_width="match_parent"
android:layout_height="684dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:layout_gravity="bottom">
<Button
android:id="@ id/send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="send" />
<EditText
android:id="@ id/typehere"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="Type here"
android:textColor="#9C9B9B" />
<Button
android:id="@ id/receive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="receive" />
</LinearLayout>
</LinearLayout>
</ListView>
my activity
public class ChatRoomActivity extends AppCompatActivity {
ListView listView;
EditText input;
Button receive;
Button send;
ArrayList<String> items;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_room);
listView = findViewById(R.id.listView);
input = findViewById(R.id.typehere);
receive = findViewById(R.id.receive);
send = findViewById(R.id.send);
items = new ArrayList<>();
items.add("I'm sending");
items.add("I'm receiving");
adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter); // see the list of result of adaptor
receive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = input.getText().toString();
addMessage(text);
input.setText("");
}
});
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = input.getText().toString();
addMessage(text);
input.setText("");
}
});
}
public void addMessage(String message){
items.add(message);
listView.setAdapter(adapter);
}
}
CodePudding user response:
after straggling in the display, I found the height for ListView is longer than my screenview, so I edit the xml to
<ListView
android:id="@ id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="50dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:layout_marginTop="-50dp"
android:orientation="horizontal"
tools:visibility="visible">
and the rest of the code didn't change I guess for others that has same issue try to shorter the listview's height
CodePudding user response:
You can use RelativeLayout
and can add android:layout_above
attribute to your ListView
Example code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@ id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@ id/llBottomLayout"/>
<LinearLayout
android:id="@ id/llBottomLayout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:orientation="horizontal"
tools:visibility="visible">
</LinearLayout>
</RelativeLayout>