Adding edittext into a custom view - java

I have a custom view that looks a little like a speedometer.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:slider="http://schemas.android.com/apk/res-auto"
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.mysite.view.SeekBar_Speedometer
android:id="#+id/slider"
slider:fill_clockwise_color="#color/lightgrey"
slider:fill_anticlockwise_color="#color/blue"
slider:fill_handle_color="#color/green"
slider:fill_handle_width="3"
slider:angle_start="120"
slider:angle_end="60"
slider:fill_start_angle="120"
slider:score_min="0"
slider:score_max="300"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
I would like to add an edittext to the center of the speedometer.
Just as you can draw text to the canvas can I add an edittext in the same way from inside my view? I don't think so!
Question is, how do I go about adding this edittext to my custom view?

inside your custom view class you can do:
EditText edit = new EditText(context);
addView( edit );
To do this your custom view must extends a ViewGroup (LinearLayout, RelativeLayout, ecc..)

Related

Clicking on Fragment goes through in Activity

I have a button in my MainActivity that opens FragmentA. FragmentA covers the whole screen, but I still see the button from MainActivity, and I can still click on it.
I've tried using clickable in my fragment layout but it's not working
MainActivty
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
val fragmentManager = this#MainActivity.supportFragmentManager
fragmentManager.beginTransaction()
.add(R.id.fragment_container, AFragment())
.addToBackStack(null)
.commit()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/fragment_container">
<Button
android:text="Button Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button" app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"/>
</android.support.constraint.ConstraintLayout>
fragment_a.xml
<?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:clickable="true"
android:focusable="true">
android:background="#color/colorPrimary"
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="A"/>
</LinearLayout>
This is happening because you placed your Button inside of the ConstraintLayout you're using as the container of your Fragment.
When you add a fragment into a container like what you're doing, it's simply adding it in the same manner as if it was a View.
Therefore, if you add a Fragment into a ConstraintLayout that already possesses a Button as a child, the Fragment will be shown alongside the Button due to ConstraintLayout allowing for overlapping Views.
This is also why, if your container was a LinearLayout, then adding a Fragment will place the fragment underneath your Button instead.
So, with that in mind, the solution would be to handle it as if they were Views.
If you added a View into a layout and you have another View overlapping, how would you get rid of it?
The most common solution would be to set the Button's visibility to INVISIBLE or GONE when the Fragment is added.
Another solution might be to raise the elevation of the Fragment, so it's now higher than your Button.
Of course, you may also remove the button from the Container and place it inside a Fragment too.
This way, you can use the replace() method in a FragmentManager to replace the Fragment containing your Button with the Fragment you want to show.

Set border on the selected image of a ViewPager

I have a viewPager with infinite loop, but i want to set a border to highlight the current image.
In my main activity i've the adapterPageAdapter and the custom ViewPager (that is clickable).
I think that is easy, but i need help.
Something like this:
Thanks
> Place image view inside another layout set the background color of
> parent layout.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/border"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="1dp">
<ImageView
android:id="#+id/imageViewImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:foregroundGravity="center_horizontal"/>
</RelativeLayout>
> inside PagerAdapter instantiateItem method when you are setting bitmap
> to imageView, also set backgroud color to layout.
final RelativeLayout border = (RelativeLayout) view.findViewById(R.id.border);
border.setBackgroundColor(Color.RED);

How to add a title to a fragment in Android

In AndroidStudio, I selected "New->Fragment->Fragment (List)" to create a list of items. It works fine and displays all the items; however, I would like to have a title at the top.
If I add another TextView to the "fragment_item_list.xml" file, then the title appears in all the items in the list. From other questions I figured that if I create another LinearLayout xml layout file (something like "list_header.xml") and put a TextView in it, I can use a layout inflater to inflate this layout in the fragment activity, e.g. in onCreateView,
View header = inflater.inflate(R.layout.list_header,container,false);
but then I don't know what to do with header to make it display somewhere in the fragment. I can add it to the original activity by calling container.addView(header); but how can I add it to the fragment?
I don't want to change the ActionBar title, firstly just for aesthetics, and secondly because the fragment covers it up. Ideally there would be a header like in an alert dialog.
Let me try to understand you. You want to add a title to your fragment list (like an header), not in the ActionBar. Right?
Looking the fragment_item_list.xml we have this: (I follow your steps: "New->Fragment->Fragment (List)")
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 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:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="#layout/fragment_item" />
The RecyclerView is a container used for populate a list, in other words, the RecyclerView (or ListView) contain all the list only. So, you have to add another container (like LinearLayout, RelativeLayout, etc.) as root of the layout to add more views to the layout. Example using the LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/text_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello World! I'm a header!"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/list"
android:name="com.veroapps.myapplication.ItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:listitem="#layout/fragment_item" />
</LinearLayout>
With this you will have a list with a title at the top. Now, you can manage the title TextView in the ItemFragment(name of the fragment generated by Android Studio). And of course there are more ways to do that, but this is an easy one.
Hope this help you.
In your Fragment java class, there should be a method called onCreateView this is where you set the layout for your Fragment (and where the list is added). It will look something like this:
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
In this Fragment the layout is inflated by this code inflater.inflate(R.layout.example_fragment, container, false); and this means there's is an xml file defining the layout called example_fragment.xml.
Your code will be the same, when you find this XML file. You can add other views inside it.
You will want to add another TextView above the RecyclerView or ListView already in that file.
Something like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
...
android:text="YOUR TITLE" />
<RecyclerView
... />
</LinearLayout>

Android : Merging two types of List with different adapters, lists get merged in UI

I am working on an Android project in which I have two different types of Chat possibilities. One is for Groups and other is for private or one to one communication. Now, all the Groups and Users the logged-in user can chat with is shown in ConversationActivity.
For this, I have prepared two lists in the UI, and contents will be added to each list by an Async method. Each List has a separate adapter, with which I will be able to easily identify, which item was clicked.
This mechanism is working just fine, except that when the activity is opened, the list look they are layered on top of each other, obviously that is not the intention. So I indicated in the RelativeLayout, to position it above another, that also didn't help.
How can I display two lists with two different adapters in one activity page?
activity_conversations.xml :
<?xml version="1.0" encoding="UTF-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip"
android:layout_above="#+id/privateRelativeLay"
>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/conversationList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip"
android:id="#+id/privateRelativeLay">
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/privateConversationList"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>
ConversationActivity.java
public class ConversationActivity extends ApplicationDrawerLoader {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_conversations);
if (isOnline()) {
new getConversationsForLoggedInUser(this).execute();
new getPrivateChannelsForLoggedInUser(this).execute();
}
public class getPrivateChannelsForLoggedInUser extends AsyncTask<Void,Void,ResponseEntity<PrivateChannel[]>>{
#Override
protected void onPostExecute(ResponseEntity<PrivateChannel[]> responseEntity) {
privateChannelConversationList = (ListView) findViewById(R.id.privateConversationList);
privateConversationAdapter = new PrivateConversationAdapter(conversationActivity, privateMapList);
privateChannelConversationList.setAdapter(privateConversationAdapter);
// On click adapter excluded
}
public class getConversationsForLoggedInUser extends AsyncTask<Void, Void, ResponseEntity<RestGroupAccount[]>> {
#Override
protected void onPostExecute(ResponseEntity<RestGroupAccount[]> responseEntity) {
super.onPostExecute(responseEntity);
groupAccountConversationList = (ListView) findViewById(R.id.conversationList);
groupConversationAdapter = new GroupConversationAdapter(conversationActivity, groupAccountMapList);
groupAccountConversationList.setAdapter(groupConversationAdapter);
// On click adpater excluded
}
}
I hope this much information is sufficient, if anything else is required, let me know. Any help would be nice. Thank you.
This is how it looks : screenshot :
They display on top of eachother because you put them both in a separate RelativeLayout that have the same positioning properties (none).
One fix would be to put both listViews in a LinearLayout instead. You can provide this LinearLayout with an orientation attribute vertical or horizontal, depending on how you want to display them, then use the layout_weight attribute to determine what portion of the screen each listviews occupies.
For example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<ListView
android:id="#+id/listView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
</LinearLayout>
Note: fill_parent is legacy. Use match_parent instead.
It is because height of your both List Views is fill_parent. Try using wrap content or in your case i don't know how exactly do you want both List views to be displayed but I would suggest you use linear layout with orientation that you want and assign weights.
The layouts stack on each other in RelativeLayout as long as the cover the whole screen width and height.
Also fill_parent attribute value is deprecated now it is better to use match_parent.

Animate EditText out of a window

I have a RelativeLayout with a "search bar" (EditText) and a ListView under it:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/etSearch"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:inputType="text" >
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="0dp"
android:clipToPadding="false"
android:listSelector="#drawable/listview_selector" >
</ListView>
</LinearLayout>
After the user "searches" I want the EditText to animate out of the screen and the ListView to push to the top. I put together a very rough GIF of what I need:
Does anyone have an idea on how I can accomplish this? thanks
UPDATE
I found out how to make the EditText animate out of the screen with this:
slide_out_top.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromYDelta="0%" android:toYDelta="-100%" android:duration="600"/>
</set>
and then using it on the EditText like this:
mSlideOutTop = AnimationUtils.loadAnimation(this, R.anim.slide_out_top);
et.startAnimation(mSlideOutTop);
but then the ListView stays at its current height. What I want is for the ListView to extend to the top as the EditText animates out.
You can add TranslateAnimation to the ViewGroup( which is LinearLayout for you), the y-axis moving distance is the hight of the editText View. Then trigger this animation when you need. (update: just thought this way may create a blank bar at bottom, bad idea)
I have an another tricky idea.
set the EditText view in the ListView with position 0 , then simply call the smoothScrollToPostion(1) method to scroll.
see this may help you
smoothScrollToPositionFromTop() is not always working like it should

Categories

Resources