text view on the image view? - java

the text should be saved on the image view ?the text should not be defined in the layout folder and should be saved on the image view on the accordingly from the key board ?
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullimage);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.fullimage);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
}

use framelayout for that .. define both ImageView and TextView and set text on TextView
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thumbHolder">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thumbImage"
android:scaleType="fitXY" />
<TextView
android:layout_width="match_parent"
android:layout_height="24dp"
android:id="#+id/titletxt"
android:gravity="center_vertical"
android:maxLines="2"
android:paddingRight="8dp"
android:paddingLeft="8dp"
android:textSize="16sp"
android:layout_gravity="bottom"
android:background="#80000000"
android:textColor="#ffffffff"
android:text="" />
</FrameLayout>
for setting custom font :-
create "assets\fonsts\" folder and then place your font there
then create a typeface by loading that font :-
Typeface tfBold = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Raleway-Bold.otf");
then set your typeface in textView :-
yourTextView.setTypeface(tfBold);

try this-
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:background="#drawable/image">
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
android:singleLine="true"
android:text="text"/>
</LinearLayout>
</LinearLayout>

You can either draw the image on a canvas, and then draw the text on the image or else, create a layout via code and set the textview inside the layout and then place the layout on top of the image view with background of the textview set to transparent

You can do it using TextView only. Using drawableTop, drawableBottom, drawableLeft, drawableRight in the following way.
<TextView
style="#style/layout_wrap"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:paddingRight="5dp"
android:drawableTop="#drawable/flags_00"
android:textColor="#color/text_color_tabs"
android:textStyle="bold"
android:maxLength="3"
android:gravity="center"
android:text="Team B" />
and in the code by the following way
battingTeamFlag.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

You can use Button and use button background to set image.and use text element for adding text on button.
<Button
android:layout_width="wrap_parent"
android:layout_height="wrap_parent"
android:text="YourText"
android:background="#drawable/image"
>
</Button>
According to your requirement you can get text from button and pass it with intent

Related

Avoid clicking of full layout

I only want my textview with id clickable_txtView to be clickable in relative layout. Here is the code -
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#android:id/empty"
android:clickable="false">
<ImageView
android:layout_width="120sp"
android:layout_height="130sp"
android:layout_centerHorizontal="true"
android:src="#drawable/flower"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="120sp"
android:text="Dont click me />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/clickable_txtView"
android:gravity="center"
android:paddingTop="140sp"
android:clickable="true"
android:text="Click me" />
</RelativeLayout>
#OnClick(R.id.clickable_txtView)
public void txtViewClick() {
//Some event
}
Thr problem is I am able to click whole relative layout and some event happens after onclicking relative layout.
How do I avoid this and make it limited to text view only.
Your textview has an attribute android:paddingTop="140sp" which basically wraps over the entire relative layout, because of which you are having the problem with onClick behaviour.
Use this RelativeLayout structure (provide ID to the "don't click me" textview, and place the "clickable textview" below it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center">
<ImageView
android:layout_width="120sp"
android:layout_height="130sp"
android:layout_centerHorizontal="true"
android:src="#mipmap/ic_launcher"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/do_not_click"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="120sp"
android:enabled="false"
android:text="Dont click me" />
<TextView
android:id="#+id/clickable_txtView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:gravity="center"
android:layout_below="#id/do_not_click"
android:text="Click me" />
</RelativeLayout>
In your activity where you are inflating this view try this:
first initialize your textview
TextView textView = (TextView) findViewById(R.id.clickable_txtView);
then apply clickListner on it
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your work here
}
});
I'm not sure but you can try adding below line to your parent layout
android:touchscreenBlocksFocus="true"
like
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:id="#android:id/empty"
android:touchscreenBlocksFocus="true">

TextView is unclickable in application

I am designing a pop up AlertDialog that sets the view of of the alert dialog to the following XML file. For some reason the file will not add margins and I cannot figure out what I am doing wrong. Margins and Padding do not work in the activity_priority_level_values LinearLayout, the LinearLayout that holds all of the TextViews, or any of the TextViews. How can I add margins (or padding) for the dialogs borders and margins between all of the text views?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_priority_level_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/large_space">
<TextView android:id="#+id/level_1_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_1_header"/>
<TextView android:id="#+id/level_1_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_1_description"/>
<TextView android:id="#+id/level_2_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_2_header"/>
<TextView android:id="#+id/level_2_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_2_description"/>
<TextView android:id="#+id/level_3_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_3_header"/>
<TextView android:id="#+id/level_3_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_3_description"/>
<TextView android:id="#+id/level_4_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_4_header"/>
<TextView android:id="#+id/level_4_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_4_description"/>
<TextView android:id="#+id/level_5_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_5_header"/>
<TextView android:id="#+id/level_5_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_5_description"/>
</LinearLayout>
</LinearLayout>
Activity
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.activity_priority_level_values);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
if(hiddenLayout == null){
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, null);
builder.setView(hiddenInfo);
}
builder.create();
builder.show();
The problem is the inflating of your view.
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, null);
You should never inflate with null. Instead change the code to:
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, myLayout, false);
If you are inflating with null the layout params will be ignored.

android - how to inflate a map fragment in an already inflated layout?

i am creating a Table Layout in my an activity, adding dynamically table rows in a loop.
What i want to achieve, is to add a Google map fragment in each dynamically created Table row.
This is my Table row Layout:
<?xml version="1.0" encoding="utf-8">
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rl_rowitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp">
<RelativeLayout
android:id="#+id/inflated_row_relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_poi_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/orange"
android:textColor="#ffffff"
android:text="Name"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="#+id/img_snapshot"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tv_poi_name"
android:adjustViewBounds="true"
android:src="#drawable/map_snapshot" />
<TextView
android:id="#+id/tv_poi_address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Address"
android:textColor="#fe8301"
android:layout_below="#+id/img_snapshot"
android:textAppearance="?android:attr/textAppearanceMedium" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/distance_layout"
android:layout_below="#+id/tv_poi_address" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/distance_icon" />
<TextView
android:id="#+id/tv_poi_distance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageView1"
android:text="Distance"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
</RelativeLayout>
</TableRow>
And this is the loop that i am creating the Table rows dynamically:
final TableLayout tblAddLayout = (TableLayout) findViewById(R.id.tl_menu_pois);
TableRow inflateRow = (TableRow) View.inflate(Menu_pois.this, R.layout.menu_pois_rowitem, null);
for (int i=0;i<3;i++){
View inflate = (TableRow) View.inflate(Menu_pois.this, R.layout.menu_pois_rowitem, >tl_menu_pois);
RelativeLayout inflated_row_relative = (RelativeLayout) >inflate.findViewById(R.id.inflated_row_relative);
inflated_row_relative.setTag(i);
TextView poi_name = (TextView) inflate.findViewById(R.id.tv_poi_name);
TextView poi_address = (TextView) inflate.findViewById(R.id.tv_poi_address);
poi_name.setText("Name");
poi_address.setText("Address");
//set tag for each TableRow
inflate.setTag(i);
//add TableRows to TableLayout
tblAddLayout.addView(inflate);
//set click listener for all TableRows
inflated_row_relative.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Object tag = v.getTag();
String string_tag = tag.toString();
}
});
}
I want to display a Google map in each Table row like the image bellow (instead of the sample image, having the map):
http://goo.gl/9erYB7
Is there a way to implement this?
Thank you very much in advance.
I found a solution for adding multiple map fragment on the same View, following this example, in case someone has the same problem:
http://saadroid.blogspot.gr/2013/06/multiple-maps-in-same-view.html

Trouble identifying the type of widget to expand/collapse an area

Consider the widget hello:
When clicked, it opens the area at the bottom with additional widgets (text, buttons, etcetera):
What is this kind of expand/collapse widget called?
One way to do this is to build your whole layout with the text and the buttons, but hide them initially by setting their visibility to gone or invisible. Then in code in the onClick listener of the "hello_text" text view, you can change their visibility to visible.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/hello_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_text" />
<TextView
android:id="#+id/the_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/my_text"
android:visibility="invisible" />
<LinearLayout android:id="#+id/btn_holder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="invisible">
<Button android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text1" />
<Button android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text2" />
</LinearLayout>
</LinearLayout>
Now in your activity or fragment you can do:
TextView the_text_view = (TextView) v.findViewById(R.id.the_text);
LinearLayout ll_btn_holder = (LinearLayout) v.findViewById(R.id.btn_holder);
TextView hello_text_view = (TextView) v.findViewById(R.id.hello_text);
hello_text_view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
the_text_view.setVisibility(View.VISIBLE);
ll_btn_holder.setVisibility(View.VISIBLE);
}
});

Unable to dynamically add EditText to LinearLayout

I have read many post similar to this, but none of them resolved my problem. Another funny thing is, on the emulator the place for the edittext is held (meaning other widgets are shifted down), but is not shown, on the other hand the same implementation on the real phone doesn't even show a place for the widget. I have the following LinearLayout inside which I need to dynamically add an EditText widget when the TextView Add a Team Member is clicked.
<LinearLayout
android:id="#+id/layout_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator"
android:layout_margin="5dp"
android:background="#color/yellow_background"
android:orientation="vertical"
android:padding="5dp">
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_check_empty" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/tv_add_team_member"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="5dp"
android:text="Add a team member"
android:textColor="#color/text_green"
android:textSize="15dp"
android:textStyle="bold" />
<LinearLayout
android:id="#+id/ll_add_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_add_team_member"
android:orientation="horizontal"
android:weightSum="100" >
<ImageView
android:id="#+id/iv_create_assignment_attach"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_flag_alt" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_weight="15"
android:text="Due"
android:textSize="15dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="65"
android:hint="None"
android:selectAllOnFocus="true" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_weight="10"
android:src="#drawable/icon_calendar_empty" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal" >
<Button
android:id="#+id/btn_create_assignment_trash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="30dp"
android:layout_marginTop="5dp"
android:text="Trash" />
<Button
android:id="#+id/btn_create_assignment_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_marginTop="5dp"
android:text="Done" />
</LinearLayout>
</LinearLayout>
Here is the Java code, the onClickListener implementation of the TextView.
tvAddTeamMember.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) getActivity().findViewById(R.id.layout_task_name);
EditText et = new EditText(getActivity());
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
et.setLayoutParams(p);
et.setText("Text");
ll.addView(et);
}
You may proceed this way ! This is not what you're looking for. But your problem will get resolved.
1) By default add the EditText view in your XML and make it invisible.
2) On Clicking the TextView, make it visible.
enjoy !
Hi #Anas Azeem: your code is correct.through your code we can add editetxt to linear layout dynamically. problem is in the layout file only.in the layout mentioned the orientation for added layout as "horizontal" ,instead of that one use "vertical" like below
<LinearLayout
android:id="#+id/layout_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/iv_check_research_tasks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<EditText
android:id="#+id/et_task_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:selectAllOnFocus="true"
android:text="Task Name"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
Try to change the line :
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
With this:
android.widget.LinearLayout.LayoutParams p= new android.widget.LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
//Then do your work
If you know you might be adding a layout, you could always declare ViewStub. After that you simply find the viewStub and call inflate() on it:
ViewStub stub = (ViewStub) findViewById(R.id.stub);
View inflated = stub.inflate();
This is the promoted way to do that in Android SDK. Thanks to that you can define the infalte layout in an xml file.
Try with this code.
replace ur line ll.addView(et);
by::
ll.addView(et,p);
and delete ur line et.setLayoutParams(p);
A simple way to do this.
Button add = (Button) findViewById(R.id.add_checkpoint);
final LinearLayout layout = (LinearLayout) findViewById(R.id.addLayout);
// layout has assigned weight = "1.0" in xml & it's a child of ScrollView
add.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater oLayoutInflater = getLayoutInflater();
// oView is declared globally & Layout "row_checkpoint" contains a EditText
oView = oLayoutInflater.inflate(R.layout.row_checkpoint, null);
final EditText speciesEditText = (EditText) oView
.findViewById(R.id.checkpoint);
speciesEditText.setId(layout.getChildCount());
// Perform whatever required over EditText, same way you can add more elements.
layout.addView(oView);
}
});
Hope it will help.

Categories

Resources