i wanted to create a terminal/console, where the user can enter commands. I know java, but i'm new to xml, so i wanted to know how i can spawn text under text and if it gets to long it should be scrollable, here's a picture:
and here's my xml cpde for it:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#d1d1d1">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:layout_width="175dp"
android:layout_height="wrap_content"
android:id="#+id/consoleText"
android:layout_gravity="bottom"
android:hint="Command"
android:layout_weight="0.99" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/sendButton"
android:layout_gravity="bottom" />
</LinearLayout>
</LinearLayout>
and here's my code for getting the text:
public class FragmentConsole extends Fragment{
EditText text;
Button button;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.console,
container, false);
button = (Button) view.findViewById(R.id.sendButton);
text = (EditText)view.findViewById(R.id.consoleText);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.v("EditText", text.getText().toString());
}
});
return view;
}
}
So i will use some photoshopped pictures to show what i want to do, so each time i enter a command in this case i will use the example "command one","command two" & "command three", so and if i click the send button after each of them they should apper like this:
so and if the text reaches this black bar:
the above added text should get pushed in a scrollview and every new text will also get part of the scrollview, so that you can scroll through your commands later on.
I know this is a long post and i hope it is clear what i want to do and someone will know how i could do this. So thanks in advance :)
For this task you should consider using ListView and add every command as new row in this view. It'll also take care for scrolling and you'r text wont colide with your EditText.
Related
I have fullscreen fragment dialog. While it is showing, background objects can be clickable or interact with user. how do i avoid this?
Here is piece of code how i show dialog:
MainActivity.java
FullScreenFrDialog fr = new FullScreenFrDialog();
FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
tr.add(android.R.id.content, fr, FR_TAG).commit();
FullScreenFrDialog.java
public class FullScreenFrDialog extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.dialog, container, false);
return view;
}
}
dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#CCFFFFFF" >
<ImageView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</FrameLayout>
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
This button clickable, while dialog is showing.
Thanks in advance.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#CCFFFFFF" >
Make the parents clickable true, hope this will help you.
In your onCreate() simply call setCancelable(false). This will prevent the dialog from closing if tapped outside the dialog and will also prevent tap events to the elements beneath
EDIT
Alternatively you can call setCanceledOnTouchOutside(false) if you want it to be dismissed with a back button press but still prevent touches to elements beneath
I try to make a button active fragment, leading to a new layout with the url of this fragment. But it is not working. Something wrong. Please help
no receive error, but the button does not work.
code fragment
public class ContentFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.content_fragment, container, false);
final View button = view.findViewById(R.id.bSendUrl);
button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText simpleEditText = (EditText) getView().findViewById(R.id.bTextUrl);
String strValue = simpleEditText.getText().toString();
Intent intent = new Intent(getActivity(), MainActivity.class);
intent.putExtra("url", strValue);
startActivity(intent);
}
}
);
return view;
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_TEXT_Url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_below="#+id/include"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:layout_marginTop="6dp">
<EditText
android:id="#+id/bTextUrl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textUri"
android:hint="#string/url_edit_text" />
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="#dimen/latimea_button_send_tv"
android:layout_height="#dimen/inaltimea_button_send_tv"
android:text="#string/button_send_android_tv"
android:id="#+id/bSendUrl"
style="?android:attr/borderlessButtonStyle"
android:layout_below="#+id/input_TEXT_Url"
android:paddingLeft="16dp"
android:layout_marginLeft="16dp" />
</RelativeLayout>
Edit
I am removed openjdk and installed clean javasdk and is work fine
You are trying to find a reference to a Button before the fragment's view hierarchy is inflated. The fragment transaction you're using does not immediately create the view hierarchy after commit() is called. This means the view with id bTextUrl is not yet created and findViewById() returns null.
Also, it's kind of strange for an activity to be messing with the views of child fragments. I recommend that you modify the views of the fragment within the fragment itself (not within the containing activity like you're doing now).
This code represent my real issue...
I'm working on an U.I. more complexe.
I have a custom view that need to have a button out of his leftSide. Why? Because i have four of this custom view aside . And those button's view to create some intercalary view !
I have a simple layout that contains a button.
I had to make him out of his layout's parent, with the property clipChildren="false"
But the button don't response to the onClickListener.
I certainly miss something, but what?
The animation click isn't played at all...
Even the Android's click's song isn't played...
The java code have no effect there..
The button's id button2 work.
The button's id button don't work..
Here is my xml code.
<LinearLayout
android:orientation="vertical"
android:background="#0000FF"
android:clipChildren="false"
android:layout_marginLeft="80dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textColor="#FF0"
android:layout_marginLeft="-20dp"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_marginLeft="-80dp"
android:layout_width="80dp"
android:layout_height="80dp" />
<Button
android:id="#+id/button2"
android:layout_width="80dp"
android:layout_height="80dp" />
</LinearLayout>
and the onCreate Methode:
Button buton1 = (Button) rootView.findViewById(R.id.button);
buton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.wtf("-----", "Click");
Toast.makeText(getActivity(), "button1", Toast.LENGTH_LONG).show();
}
});
Button buton2 = (Button) rootView.findViewById(R.id.button2);
buton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "button2", Toast.LENGTH_LONG).show();
}
});
Try putting the button back inside its parent.
If it works there, have a very long hard think about why you want this button to not live inside its parent. This is a bad idea almost all of the time as it breaks things (both practically and conceptually).
I would suspect the issue is that something else which has a better claim to the space the button is occupying is consuming the touch event.
I have been playing around a lot with the ExpandableListView and I cannot figure out where to add the button listeners for the button that will be the children in the view. I did manage to get a button listener working that uses getChildView() below, but it seems to be the same listener for all the buttons.
The best case scenario is that I would be able to implement the button listeners in the class that instantiates the ExpandableListAdapter class, and not have to put the listeners in the actual ExpandableListAdapter class. At this point I don't even know if that is possible
I have been experimenting with this tutorial/code: HERE
getChildView()
#Override
public View getChildView(int set_new, int child_position, boolean view, View view1, ViewGroup view_group1)
{
ChildHolder childHolder;
if (view1 == null)
{
view1 = LayoutInflater.from(info_context).inflate(R.layout.list_group_item_lv, null);
childHolder = new ChildHolder();
childHolder.section_btn = (Button)view1.findViewById(R.id.item_title);
view1.setTag(childHolder);
childHolder.section_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Toast.makeText(info_context, "button pushed", Toast.LENGTH_SHORT).show();
}
});
}else {
childHolder = (ChildHolder) view1.getTag();
}
childHolder.section_btn.setText(children_collection.get(set_new).GroupItemCollection.get(child_position).section);
Typeface tf = Typeface.createFromAsset(info_context.getAssets(), "fonts/AGENCYR.TTF");
childHolder.section_btn.setTypeface(tf);
return view1;
}
Any help would be much appreciated. Thank you and I will be standing by.
If the buttons are in the ExpandableListView, their listener needs to be in the adapter.
I'm not sure the thrust of your question, but if you are asking how do you relate the button to the contents of the child row, I can answer that. :p
I'll assume a somewhat simple child row layout for demonstration purposes.
child_row.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="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:id="#+id/ListItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left|center_vertical"
android:paddingLeft="7dip"
android:paddingRight="7dip"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ListItem2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right|center_vertical"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Then, to get the contents of the row when your button is pressed, you use the button to backtrack to the parent vieew and then get the necessary child views and their contents:
childHolder.section_btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
LinearLayout ll = (LinearLayout) v.getParent(); // get the view containing the button
TextView tv1 = (TextView) ll.findViewById(R.id.ListItem1); // get the reference to the first widget
TextView tv2 = (TextView) ll.findViewById(R.id.ListItem2); // get the reference to the second widget
String text1 = tv1.getText.toString(); // Get the contents of the first widget to a string
String text2 = tv2.getText.toString(); // Get the contents of the second widget to a string
}
});
If this isn't what you were looking for clarify your question and I'll take another shot at it.
What I have is a canvas that takes up nearly all of the screen, then under that I want a row of buttons and some other widgets. So I did this.
XML Code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/myLayout"
android:orientation="vertical"
android:layout_height="match_parent" >
<com.zone.manager.Tab3
android:id="#+id/tab3_display"
android:layout_width="fill_parent"
android:layout_height="620dp" />
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="match_parent" >
<Button
android:id="#+id/addZone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Zone" />
<Button
android:id="#+id/helpZone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Help" />
<SeekBar
android:id="#+id/seekBar1"
android:paddingTop="9dp"
android:layout_width="179dp"
android:layout_height="wrap_content" />
</LinearLayout>
Java Code
public class Tab3 extends View implements OnTouchListener, OnClickListener {
public Tab3(Context context, AttributeSet attrs) {
View parent = (View) getParent();
addZone = (Button) parent.findViewById(R.id.addZone);
addZone.setOnClickListener(this);
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
. . . draws a bunch of stuff
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.addZone:
String w = "W~20~0~0~0~0~0";
Log.d("ZoneSize", "Zone set");
MyApplication.preferences.edit().putString( "ZoneSize", w ).commit();
MyApplication.preferences.edit().putBoolean("ZoneSizeReady", true).commit();
break;
}
}
However my problem with this is that I believe the code is not reconising where the addZone is. Because when I have the addZone.setOnClickListener active my program will crash, but when I don't have it active the layout looks like how I want it. What must I do to fix this?
addZone = (Button) findViewById(R.id.addZone);
addZone will be null because com.zone.manager.Tab3 does not have any children
So it is obvious that your code will crash
So either you will give com.zone.manager.Tab children which require also to change the base class from View to ViewGroup,
or you start with com.zone.manager.Tab parent. Something like
View parent = (View) getParent ();
addZone = (Button) parent.findViewById(R.id.addZone);
i have some tips which will make you avoid such weird bugs and others:
the code of the custom view relies on an xml layout that uses the custom view .
this is bad coding.
instead, you should use LayoutInflater , use a layout xml file for the custom view for it , and then do the "findViewById" and add the clickListeners to any view inside that you wish.
i think it's also wrong to set the custom view to hold the click listener of the other views without checking which of them was clicked . either add a check , or add a different listener for each of them (which i personally prefer) .