Created
November 14, 2017 17:59
-
-
Save sankarcheppali/a1f1b15fafa1c8886668356ce969a2ed to your computer and use it in GitHub Desktop.
Text view On click : Show list view : On click item : show selected item in TextView
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.anhure.test3.test3android; | |
import android.content.Context; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.BaseAdapter; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
/** | |
* Created by sanka on 11/14/2017. | |
*/ | |
class CustomAdapter extends BaseAdapter { | |
private String[] name; | |
private MainActivity main; | |
public CustomAdapter(MainActivity mainActivity, String[] names) { | |
this.main=mainActivity; | |
this.name=names; | |
} | |
@Override | |
public int getCount() { | |
return name.length; | |
} | |
@Override | |
public Object getItem(int position) { | |
return null; | |
} | |
@Override | |
public long getItemId(int position) { | |
return 0; | |
} | |
@Override | |
public View getView(int position, View convertView, ViewGroup parent) { | |
LayoutInflater inflater = main.getLayoutInflater(); | |
View rowView= inflater.inflate(R.layout.rowlayout, null, true); | |
TextView textView = (TextView) rowView.findViewById(R.id.text_value); | |
textView.setText(name[position]); | |
return rowView; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.anhure.test3.test3android; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.AdapterView; | |
import android.widget.ListView; | |
import android.widget.TextView; | |
public class MainActivity extends AppCompatActivity { | |
TextView text; | |
ListView list; | |
public String [] names={"krishna","kumar","kavin"}; | |
String TAG="TEST3ANDROID:MainActv"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
list=(ListView)findViewById(R.id.list); | |
text=(TextView)findViewById(R.id.text); | |
CustomAdapter adapter = new CustomAdapter(MainActivity.this,names); | |
list.setAdapter(adapter); | |
list.setVisibility(View.GONE); | |
text.setVisibility(View.VISIBLE); | |
text.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Log.d(TAG,"TextVIew is cliked"); | |
v.setVisibility(View.GONE); | |
list.setVisibility(View.VISIBLE); | |
} | |
}); | |
list.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
Log.d(TAG,"Item selected in the list :"+position); | |
text.setText("Selected Item "+names[position]); | |
list.setVisibility(View.GONE); | |
text.setVisibility(View.VISIBLE); | |
} | |
}); | |
list.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | |
@Override | |
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { | |
Log.d(TAG,"Item selected in the list :"+position); | |
text.setText("Selected Item "+position); | |
list.setVisibility(View.GONE); | |
text.setVisibility(View.VISIBLE); | |
} | |
@Override | |
public void onNothingSelected(AdapterView<?> parent) { | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment