Read More button in android - android

I have looked through few read more button questions but I didnot get a kind of solution that I was looking more. I want to display only certain part of layout and only when user presses the Read more button would display the rest of the layout with drop down animation. Something of this sort:

I cant give you the exact code u desires, but i can help u by giving a sample code that meets your requirement,
Here I am only describe that how more/less button functionality are work.
first of all in your activity_main.xml file paste below code.
<ScrollView 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" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/description_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:maxLines="5"
android:text="#string/desc_content" />
<ImageButton
android:id="#+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/description_text"
android:background="#drawable/arrow_down"
android:clickable="true" />
<View
android:id="#+id/view1"
android:layout_width="wrap_content"
android:layout_height="2dp"
android:layout_below="#+id/description_text"
android:layout_marginTop="5dp"
android:layout_toLeftOf="#+id/show"
android:background="#000" />
<ImageButton
android:id="#+id/hide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/description_text"
android:background="#drawable/arrow_up"
android:clickable="true"
android:visibility="invisible" />
</RelativeLayout>
</ScrollView>
MainActivity.java
package com.example.expand.textview;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView descText;
ImageButton show, hide;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
descText = (TextView) findViewById(R.id.description_text);
show = (ImageButton) findViewById(R.id.show);
show.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Show button");
show.setVisibility(View.INVISIBLE);
hide.setVisibility(View.VISIBLE);
descText.setMaxLines(Integer.MAX_VALUE);
}
});
hide = (ImageButton) findViewById(R.id.hide);
hide.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Hide button");
hide.setVisibility(View.INVISIBLE);
show.setVisibility(View.VISIBLE);
descText.setMaxLines(5);
}
});
}
}
and finally change your string.xml file
string.xml
<resources>
<string name="app_name">Expand TextView</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
<string name="desc_content"> Android powers hundreds of millions of mobile devices in more than 190 countries around the world. It\'s the largest installed base of any mobile platform and growing fast—every day another million users power up their Android devices for the first time and start looking for apps, games, and other digital content.
Android gives you a world-class platform for creating apps and games for Android users everywhere, as well as an open marketplace for distributing to them instantly.
</string>
</resources>
and run your Project It will give you below output.

Related

android setOnClickListener it doesn't work

Nothing happens when i press to button. How can i fix that. I'm new on Java and I couldn't find my mistake.
click to see the video
package com.example.tolgadogan.myapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Button;
public class clickj extends Activity {
Button wrt;
TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
wrt = (Button)findViewById(R.id.button);
txt = (TextView) findViewById(R.id.textView);
wrt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
txt.setText("This is a test text...");
}
});
}
}
And Also XML code is here;
<?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: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="com.example.tolgadogan.myapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Text"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:id="#+id/button"
android:layout_marginTop="35dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I did everything and exactly same code with what i see in tutorial videos but it doesn't work when i press to button.
Its because that view is connected with the MainActivity. But you add the onClick in the otherActivity(SecondActivity) and you set the same view to that activity as well. When you run the app activity with the LAUNCHER starts the app which is your MainActivity.It has a view.But no click events.
Go with one of these options and add some music to your video :P
Add your click events in your MainActivity
Make your SecondActivity starts first by making it the launcher activity
Call SecondActivity from your MainActivity using an intent.Then you can perform any actions that you have in that activity
This is the launcher check it in your manifest.xml
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Then your click will work

How to use onClick function to make a button show a text view [duplicate]

This question already has an answer here:
Which is the best way to add a button?
(1 answer)
Closed 6 years ago.
I'm really sorry because I'm writing this after a lot of similar questions that (I guess you know) already exist, but I need your help. I tried different methods as described in other posts, but none work for me. I'm new to Android development and I've have a simple "Hello World!" app. I customized it with a custom material design theme and a button, also customized. I was trying to use the "onClick" function to show a TextView when the button is clicked, but I don't know how to do it. Here is my Relative Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TextView
android:id="#+id/helloworld1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:text="#string/hello_world"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/press_here"
android:textColor="#color/White"
android:background="#color/Red"
android:translationY="40dp"/>
And here is my MainActivity.java:
package com.giuskiller.material;
import android.app.*;
import android.os.*;
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
You need to define a method that will show the TextView, then assign that method to the onClick attribute of your Button, like so:
Layout XML:
<TextView
android:id="#+id/helloworld1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button1"
android:text="#string/hello_world"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="#string/press_here"
android:textColor="#color/White"
android:onClick="showTextView"
android:background="#color/Red"
android:translationY="40dp"/>
Activity:
package com.giuskiller.material;
import android.app.; import android.os.;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void showTextView(View view){
findViewById(R.id.helloworld1).setVisibility(View.VISIBLE);
}
}

ListView using array adapter and customizing xml file

I am trying to show a simple listView with an image, a large text and a small text in each list item. I have coded the following but when I run the app, it gives a NullPointerException. For the method i have used, I do not need to findviewbyid so I'm not sure what I am doing to give this error. Anyone know?
This is my MenuPage.java file
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
public class MenuPage extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[]values=new String[]{"Coffee","Steak","Ice Cream"};
String[]prices=new String[]{"$4.99","$21.99","$12.99"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.values, values));
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.prices, prices));
}
}
this is my menu.xml for the java file above:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ImageView
android:id="#+id/ic_launcher_movies"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="4dp"
android:layout_marginRight="10px"
android:layout_marginTop="2dp"
android:src="#drawable/items"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageView>
<TextView
android:id="#+id/values"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/values"
android:textSize="25sp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="64dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:minHeight="120dp">
</TextView>
<TextView
android:id="#+id/prices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/prices"
android:textSize="10sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="120dp"
android:layout_marginLeft="65dp">
</TextView>
also, I created an xml with the drawables for the images i want to use;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:duration="30"
android:drawable="#drawable/coffee"/>
<item
android:duration="30"
android:drawable="#drawable/steak"/>
<item
android:duration="30"
android:drawable="#drawable/icecream"/>
and my logcat gives this error
any help would be appreciated! thank you
I think the problem in the menu layout file
android:text="#+id/values"
try to write some text or specify link to string resource like
android:text="#string/my_button_text"
And when you call setListAdpate two times you ovirwrite first adapter with new. Have a look to example here
ListActivity

AutocompleteTextView filled with Google Search results

I need to have a TextView that, when the user type in it a word, a dropdown list box is be shown exactly with the same phrases that Google Search will suggest if those words are typed in the search textbox of google.com site.
Is it possible to have this service. How?
I searched on the web with no luck.
Thank you all.
For the dropdown you can use:
Autocomplete Android
For show result you must call your own web service or cached data. If you need google results i don't know if google expose a public API to obtain it...
Hi Geltrude here i added the sample source code for you. Please try and let me know. Hope it should helpful for you. Thanks.
First create autocomplete_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<AutoCompleteTextView
android:id="#+id/autoCompleteText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="133dp"
android:ems="10"
android:hint="Search Name"
android:paddingLeft="10dp"
android:paddingTop="7dp"
android:singleLine="true" />
</RelativeLayout>
list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16sp"
android:textStyle="bold" >
</TextView>
AutocompleteSample.java
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
public class AutocompleteSample extends Activity {
private static AutoCompleteTextView autoCompleteText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.autocomplete_textview);
autoCompleteText = (AutoCompleteTextView)findViewById(R.id.autoCompleteText);
List<String> nameList = new ArrayList<String>();
nameList.add("Steve");
nameList.add("Bill");
nameList.add("Tom");
nameList.add("Arnold");
nameList.add("Vishva");
nameList.add("Durai");
nameList.add("John");
nameList.add("Sam");
nameList.add("Johnson");
nameList.add("Hentry");
nameList.add("Thamos");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
R.layout.list_item, nameList);
autoCompleteText.setAdapter(dataAdapter);
}
}

Android - Why after creating new objects it says "cannot be resolved or not a field"?

I'm very new to Android developing.
When I create a new project and try to simply add a button using the graphical layout and try refering to it it won't find it.
Sometimes doing a "Clean..." works sometimes it doesn't. Also sometimes restarting eclipse works and sometimes it doesn't.
Is eclipse really that unstable or am I doing something wrong?
Also I should mention that I did go over all the posts of other people (I know this question is being asked alot) but I still haven't found an asnwer
Here is the .java file:
package com.example.testapp;
import com.example.testapp.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.button1);
}
}
and here is the xml file:
<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" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="14dp"
android:text="Button" />
</RelativeLayout>

Categories

Resources